﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Windows;
using System.Collections.ObjectModel;
using MSSClient.Model;

/// <summary>
/// TreeModel
/// </summary>
namespace TestTreeView.Model
{
    [Serializable]
    public class TreeModel : INotifyPropertyChanged
    {
        #region 私有变量
        /// <summary>
        /// Id值
        /// </summary>
        private string _id;
        /// <summary>
        /// 显示的名称
        /// </summary>
        private string _name;
        /// <summary>
        /// 图标路径
        /// </summary>
        private string _icon;
        /// <summary>
        private string _date;
        /// 选中状态
        /// </summary>
        private bool _isChecked;
        /// <summary>
        /// 折叠状态
        /// </summary>
        private bool _isExpanded;
        /// <summary>
        /// 子项，注意：声明为ObservableCollection类型，可以把数据注册观察者模式，
        /// 只要引用的数据指针指向这个内存地址，当数据发生变化则会刷新TreeView的UI
             /// </summary>
        private ObservableCollection<TreeModel> _children;
        /// <summary>
        /// 父项
        /// </summary>
        private TreeModel _parent;

        public InvoiceModel invoiceModel;

        private String m_strImageUrl;
        public System.String ImageUrl
        {
            get { return m_strImageUrl; }
            set { m_strImageUrl = value; }
        }

        private String m_strBarcode = "";
        public System.String Barcode
        {
            get { return m_strBarcode; }
            set { m_strBarcode = value; }
        }
        #endregion

        /// <summary>
        /// 构造
        /// </summary>
        public TreeModel()
        {
            invoiceModel = new InvoiceModel();
            Children = new ObservableCollection<TreeModel>();
            _isChecked = false;
            IsExpanded = false;
            _icon = "";
        }

        /// <summary>
        /// 键值
        /// </summary>
        public string Id  //节点ID
        {
            get { return _id; }
            set { _id = value; }
        }

        /// <summary>
        /// 显示的字符
        /// </summary>
        public string Name //节点名称
        {
            get { return _name; }
            set
            { 
                _name = value; 
                NotifyPropertyChanged("Name");
            }
        }

        /// <summary>
        /// 图标
        /// </summary>
        public string Date 
        {
            get { return _date; }
            set { _date = value; }
        }

        public int level = 0;//层级，1-根节点(类型)，2-二级节点(分组)，3-三级节点(图像)

        public int isDelete = 0;//补单操作，是否删除,1-删除，0或者空-不删除

        public string ftpUrl;//ftp路径

        public string httpUrl;//http路径

        public string imageName;//图像名称

        public string imageCode;//报账单号

        public string repImageCode;//子流程单号

        public int ChildrenCount;//子数量

        public string bizId;

        public string Type;//类型

        public int isBiSaoType = 0;//是否是补单任务单据，1-是，0或者空-不是

        public string ParentName;//父节点名称

        public string UploadRes;//能否上传结果

        public int isMerge = 0;//记录 是否是 合并图像的节点，1-是，0或者空-不是

        public int isBuSaoTag = 0;//是否补扫，1-是，0或空-不是

        public int isNeedScan;//是否需要扫描，1-需要，0或者空-不需要

        public int evaResult;//补单方式 1=仅删除，2=补扫，3=仅补纸质，4=删除且补扫，5=删除且仅补纸质，-1=退单

        public string Description = "";//评价描述

        public string Notation = "";//批注

        public int isFail = 0;//是否上传失败 1-失败，0-成功

        public int isIdentifying = 0;//是否标识为发票 0-否，1-是

        private int isOCRSuccess = 0;//是否识别出发票信息 0-否，1-是

        public int IsClientOcr = 0;//是否是客户端识别发票 0-否，1-是

        public int count = 0;//序号

        public String imageScanTime = "";

        public Boolean InsertTag = false;

        public String InsertPoint = "";

        public List<string> nameTag = new List<string>();//名称后缀标识，如[补]，[发票]，[合]，[删]，[专用发票]，[普通发票]，[电子发票]等
        /// <summary>
        /// 是否选中
        /// </summary>
        public bool IsChecked
        {
            get
            {
                return _isChecked;
            }
            set
            {
                if (value != _isChecked)
                {
                    _isChecked = value;
                    NotifyPropertyChanged("IsChecked");

                    if (_isChecked)
                    {
                        //如果选中则父项也应该选中
                        if (Parent != null)
                            Parent.IsChecked = true;
                    }
                    else
                    {
                        //如果取消选中子项也应该取消选中
                        foreach (TreeModel child in Children)
                            child.IsChecked = false;
                    }
                }
            }
        }

        /// <summary>
        /// 是否展开
        /// </summary>
        public bool IsExpanded
        {
            get { return _isExpanded; }
            set
            {
                if (value != _isExpanded)
                {
                    //折叠状态改变
                    _isExpanded = value;
                    NotifyPropertyChanged("IsExpanded");
                }
            }
        }

        /// <summary>
        /// 父项
        /// </summary>
        public TreeModel Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }

        /// <summary>
        /// 子项
        /// </summary>
        public ObservableCollection<TreeModel> Children
        {
            get { return _children; }
            set
            {
                _children = value;
                NotifyPropertyChanged("Children");
            }
        }

        public InvoiceModel InvoiceModelFun
        {
            get { return invoiceModel; }
            set
            {
                invoiceModel = value;
                NotifyPropertyChanged("InvoiceModel");
            }
        }

        /// <summary>
        /// 设置所有子项的选中状态
        /// </summary>
        /// <param name="isChecked"></param>
        public void SetChildrenChecked(bool isChecked)
        {
            foreach (TreeModel child in Children)
            {
                child.IsChecked = IsChecked;
                child.SetChildrenChecked(IsChecked);
            }
        }

        /// <summary>
        /// 设置所有子项展开状态
        /// </summary>
        /// <param name="isExpanded"></param>
        public void SetChildrenExpanded(bool isExpanded)
        {
            foreach (TreeModel child in Children)
            {
                child.IsExpanded = isExpanded;
                child.SetChildrenExpanded(isExpanded);
            }
        }

        private bool _isSelected;

        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (_isSelected != value)
                {
                    _isSelected = value;
                    NotifyPropertyChanged("IsSelected");
                }
            }
        }

        /// <summary>
        /// 是否标识为发票
        /// </summary>
        public int IsIdentifying { get => isIdentifying; set => isIdentifying = value; }

        /// <summary>
        /// 是否识别出发票信息
        /// </summary>
        public int IsOCRSuccess { get => isOCRSuccess; set => isOCRSuccess = value; }
        public int imageOrder { get; internal set; }

        /// <summary>
        /// 属性改变事件
        /// </summary>
        [field:NonSerializedAttribute()] 
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
        }

        internal bool Contains(TreeModel s_selectedTreeModel)
        {
            throw new NotImplementedException();
        }
    }

    [Serializable]
    public class GroupModel
    {
        public IList<TreeModel> TreeList  = new List<TreeModel>();
        public string ImageCode;
        public string bizId;
        public string reqCode;
        public string UploadResult;
        public int isBuSaoTag;
        public string Id;
        public string type;
        public string insertPoint = "";
        public bool insertTag = false;
        public int logMark;//1-上传成功，2-上传失败
        public int isDelete;//补单操作，是否删除
        public string ftpUrl;//ftp路径
        public string httpUrl;//http路径
    }
}
